﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using TheDrinksApp.Database;
using GestureEventArgs = System.Windows.Input.GestureEventArgs;

namespace TheDrinksApp.Screen.RecentOrder.TableViewCell
{
    public partial class RecentOrderRow : UserControl
    {
        private Models.Order order;
        private RecentOrderScreen container;

        public RecentOrderRow(RecentOrderScreen container, Models.Order order)
        {
            InitializeComponent();
            this.container = container;
            this.order = order;
            name.Text = order.OrderName;
        }

        private void OnFlick(object sender, FlickGestureEventArgs e)
        {
            if ((e.Angle >= 340 && e.Angle <= 360) || (e.Angle >= 0 && e.Angle <= 20) || (e.Angle >= 160 && e.Angle <= 200))
            {
                if (e.HorizontalVelocity < 0 || e.HorizontalVelocity > 0)
                {
                    Visibility prevState = rename.Visibility;
                    container.showAllRenameButton();
                    toggleButton(prevState);
                }
            }
        }

        private void toggleButton(Visibility prevState)
        {
            if (prevState == Visibility.Visible && name.IsEnabled == false)
            {
                showDeleteButton();
            }
            else
            {
                showRenameButton();
            }
        }

        public void showDeleteButton()
        {
            rename.Visibility = Visibility.Collapsed;
            btnDelete.Visibility = Visibility.Visible;
        }

        public void showRenameButton()
        {
            rename.Visibility = Visibility.Visible;
            btnDelete.Visibility = Visibility.Collapsed;
        }

        private void rename_Tap(object sender, GestureEventArgs e)
        {
            e.Handled = true;
            name.IsEnabled = true;
            name.Focus();
            btnGoToDetail.Visibility = Visibility.Collapsed;
        }
        private void name_LostFocus(object sender, RoutedEventArgs e)
        {
            name.IsEnabled = false;
            name.Text = order.OrderName;
            btnGoToDetail.Visibility = Visibility.Visible;
        }

        private void name_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                saveOrderName();
                name.IsEnabled = false;
                btnGoToDetail.Visibility = Visibility.Visible;
            }
        }

        private void name_Tap(object sender, GestureEventArgs e)
        {
            
            e.Handled = true;
           
        }

        private void saveOrderName()
        {
            order.OrderName = name.Text;
            Util.UpdateOrder(order);
        }

        private void btnDelete_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	Util.DeleteOrderWithId(order.OrderID);
            container.removeItem(this);
        }

        private void btnGoToDetail_Click(object sender, RoutedEventArgs e)
        {
            container.showOrderDetails(order);
        }
    }
}
